﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Drawing.Drawing2D;

namespace OpenComposite.Workflow
{
	[Designer(typeof(HorizontalFaultHandlersDesigner))]
	[AlternateFlowActivity()]
	public partial class HorizontalFaultHandlersActivity: HorizontalSequenceActivity, IActivityEventListener<ActivityExecutionStatusChangedEventArgs>
	{
		protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
		 {
			Exception e = Parent.GetValue(ActivityExecutionContext.CurrentExceptionProperty) as Exception;
			if ( e != null ) {
				Type t = e.GetType();
				foreach ( HorizontalFaultHandlerActivity activity in EnabledActivities ) {
					if ( t == activity.FaultType || t.IsSubclassOf(activity.FaultType) ) {
						Parent.RemoveProperty(ActivityExecutionContext.CurrentExceptionProperty);
						activity.SetValue(HorizontalFaultHandlerActivity.FaultProperty, e);
						activity.RegisterForStatusChange(Activity.ClosedEvent, this);
						executionContext.ExecuteActivity(activity);
						return ActivityExecutionStatus.Executing;
					}
				}
			}
			return ActivityExecutionStatus.Closed;
		}

		#region IActivityEventListener<ActivityExecutionStatusChangedEventArgs> Members

		void IActivityEventListener<ActivityExecutionStatusChangedEventArgs>.OnEvent(object sender, ActivityExecutionStatusChangedEventArgs e)
		{
			ActivityExecutionContext context = sender as ActivityExecutionContext;
			e.Activity.UnregisterForStatusChange(Activity.ClosedEvent, this);
			WorkflowHelpers.SecureCloseContext(context);
		}

		#endregion
	}

	[ActivityDesignerTheme(typeof(HorizontalFaultHandlersDesignerTheme))]
	public class HorizontalFaultHandlersDesigner:ActivityPreviewDesigner
	{
		public override bool CanInsertActivities(HitTestInfo insertLocation, System.Collections.ObjectModel.ReadOnlyCollection<Activity> activitiesToInsert)
		{
			foreach ( Activity activity in activitiesToInsert ) {
				if ( !( activity is HorizontalFaultHandlerActivity ) ) {
					return false;
				}
			}
			return base.CanInsertActivities(insertLocation, activitiesToInsert);
		}

		protected override void Initialize(Activity activity)
		{
			base.Initialize(activity);
			this.ShowPreview = false;
			this.HelpText = "Drop Fault Handlers here";
		}

		public override bool CanExpandCollapse
		{
			get
			{
				return false;
			}
		}
	}

	public class HorizontalFaultHandlersDesignerTheme : ActivityPreviewDesignerTheme
	{
		public HorizontalFaultHandlersDesignerTheme(WorkflowTheme theme):base(theme)
		{
			this.BackColorStart = Color.Firebrick;
			this.BackColorEnd = Color.White;
			this.BackgroundStyle = LinearGradientMode.BackwardDiagonal;
			this.PreviewBackColor = Color.Transparent;
		}
	}
}
